<?php
namespace app\common\model\mysql;


class Goods extends BaseModel
{
    protected $name = 'mall_goods';

    protected $pk = 'id';

    //商品状态
    const STATUS_NORMAL = 1;
    const STATUS_PEDDING = 0;
    const STATUS_DELETE = 99;


    //规格类型
    const GOODS_SPECS_TYPE_UNITE = 1;
    const GOODS_SPECS_TYPE_MULTI = 2;

    static $statusArr = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_PEDDING => '待审核',
        self::STATUS_DELETE => '删除',
    ];

    protected $autoWriteTimestamp = true;

    public function updateById($id, $data)
    {
        $data['update_time'] = time();
        return $this->where(['id' => $id])->save($data);
    }


    public function searchTitleAttr($query, $value)
    {
        $query->where('title','like','%'.$value.'%');
    }


    public function searchCreateTimeAttr($query, $value)
    {
        $query->whereBetweenTime('create_time',$value[0], $value[1]);
    }


    public function getLists($keys, $data, $num)
    {
        $order = [
            'listorder' => 'desc',
            'id' => 'desc',
        ];

        if (!empty($keys)) {
            $res = $this->withSearch($keys, $data);
        } else {
            $res = $this;
        }

        $result = $res->where('status', '<>', self::STATUS_DELETE)
            ->order($order)
            ->paginate($num);
        return $result;
    }
}